﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCS.IConnect.Architecture.Server;
using Microsoft.Practices.ObjectBuilder2;
using JM.JCY.Common;
using Microsoft.SharePoint;

namespace JM.JCY.BusinessFacade
{
    public class JCYBusinessFacadeBase:BusinessFacadeBase
    {

    }

    public class JCYBusinessFacadeBase<TBusinessComponent> : JCYBusinessFacadeBase where TBusinessComponent : class
    {
        /// <summary>
        /// Gets or sets the business component.
        /// </summary>
        /// <value>The business component.</value>
        public TBusinessComponent BusinessComponent
        { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="OCPBusinessFacadeBase&lt;TBusinessComponent&gt;"/> class.
        /// </summary>
        public JCYBusinessFacadeBase()
        {
            try
            {
                if (string.IsNullOrEmpty(ApplicationContext.UserName))
                {
                    ApplicationContext.UserName = SPContext.Current.Web.CurrentUser.LoginName; //what happens when i do a jQuery ajax?                     
                }
            }
            catch
            {
                ApplicationContext.UserName = "System Account";
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="OCPBusinessFacadeBase&lt;TBusinessComponent&gt;"/> class.
        /// </summary>
        /// <param name="bc">The bc.</param>
        public JCYBusinessFacadeBase(TBusinessComponent bc)
        {
            Guard.ArgumentNotNull(bc, "bc");
            this.BusinessComponent = bc;
        }

        protected JCYTransaction NewTransaction()
        {
            return NewTransaction(string.Empty);
        }

        protected JCYTransaction NewTransaction(string auditFunctionName)
        {
            JCYTransaction t = new JCYTransaction(auditFunctionName, ApplicationContext.UserName);
            ApplicationContext.TransactionId = t.transactionID;

            return t;
        }
    }
}

